package com.company.project.service.impl;

import com.company.project.dao.DictionaryMapper;
import com.company.project.model.Dictionary;
import com.company.project.model.Periodicals;
import com.company.project.service.DictionaryService;
import com.company.project.core.AbstractService;
import com.company.project.util.PageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by CodeGenerator on 2018/10/08.
 */
@Service
@Transactional
public class DictionaryServiceImpl extends AbstractService<Dictionary> implements DictionaryService {
    @Resource
    private DictionaryMapper dictionaryMapper;

    /**
     * 查询期刊
     *
     * @return
     */
    @Override
    public Map<String, List<Dictionary>> getDic() {
        List<Dictionary> dicList = dictionaryMapper.selectDic();
        Map<String, List<Dictionary>> map = new HashMap<>();
        for (Dictionary dic : dicList) {
            map.put(dic.getType(), null);
        }
        for (Map.Entry entry : map.entrySet()) {
            List<Dictionary> d = new ArrayList<>();
            for (Dictionary dic : dicList) {
                if (entry.getKey().equals(dic.getType())) {
                    d.add(dic);
                }
            }
            entry.setValue(d);
        }

        return map;
    }

    /**
     * 后台查询字典数量
     * @return
     */
    @Override
    public Integer DicAllNum() {
        return dictionaryMapper.DicAllNum();
    }

    /**
     * 后台查询期刊
     *
     * @return
     */
    @Override
    public Map<String, Object> adminSelectDic(Integer index) {
        PageUtil<Dictionary> pageUtil = new PageUtil();
        pageUtil.setPageSize(15);
        pageUtil.setCpage(index);
        pageUtil.setTotalRow(this.DicAllNum());
        List<Dictionary> dictionaries = dictionaryMapper.adminSelectDic(pageUtil);
        Map<String, Object> map = new HashMap<>();
        for (Dictionary dic : dictionaries) {
            map.put(dic.getType(), null);
        }
        for (Map.Entry entry : map.entrySet()) {
            List<Dictionary> d = new ArrayList<>();
            for (Dictionary dic : dictionaries) {
                if (entry.getKey().equals(dic.getType())) {
                    d.add(dic);
                }
            }
            entry.setValue(d);
        }
        map.put("totalPage",pageUtil.getTotalPage());
        return map;
    }

    /**
     * 后台添加期刊字典
     * @param dictionary
     * @return
     */
    @Override
    public Boolean adminAddDic(Dictionary dictionary) {
        return dictionaryMapper.adminAddDic(dictionary);
    }

    /**
     * 后台修改期刊字典
     * @param dictionary
     * @return
     */
    @Override
    public Boolean adminUpDic(Dictionary dictionary) {
        return dictionaryMapper.adminUpDic(dictionary);
    }

    /**
     * 后台删除期刊字典
     * @return
     */
    @Override
    public Boolean adminDelDic(Integer id) {
        return dictionaryMapper.adminDelDic(id);
    }

    /**
     * 后台查询所有类型
     */
    @Override
    public List<Dictionary> selectType() {
        return dictionaryMapper.selectType();
    }
    /**
     * 根据id查询字典详情
     */
    @Override
    public List<Dictionary> DMParticular(Integer id) {
        return dictionaryMapper.DMParticular(id);
    }

    /**
     * 查询期刊等级
     * @return
     */
    @Override
    public List<Dictionary> AdminRank() {
        return dictionaryMapper.AdminRank();
    }
    /**
     * 查询发刊周期
     */
    @Override
    public List<Dictionary> AdminPeriod() {
        return dictionaryMapper.AdminPeriod();
    }
    /**
     * 查询影响因子
     */
    @Override
    public List<Dictionary> AdminAffect() {
        return dictionaryMapper.AdminAffect();
    }


}
